package com.gitee.wsl.android.ext

import android.app.Activity
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Resources
import android.os.Build
import android.util.DisplayMetrics
import android.util.Size
import android.view.WindowManager
import com.gitee.wsl.android.server.alter.ReschedulingReceiver
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview


val displayMetrics = Resources.getSystem().displayMetrics

/**
 * get the screen size including navigation bar
 */
val Context.screenSize: Size
    get() =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            this.getSystemService(WindowManager::class.java).currentWindowMetrics.let { Size(it.bounds.width(), it.bounds.height()) }
        } else {
            val display = getSystemService(WindowManager::class.java).defaultDisplay
            val metrics = if (display != null) DisplayMetrics().also { display.getRealMetrics(it) } else resources.displayMetrics
            Size(metrics.widthPixels, metrics.heightPixels)
        }


/**
 * 判断Activity 是否可用
 */
fun Context?.isActivityAvailable(): Boolean {
    if (null == this) return false
    if (this !is Activity) return false
    if (this.isFinishing) return false
    return !( this.isDestroyed)
}


/**
 * get displayMetrics in Context
 */
val Context.displayMetrics: DisplayMetrics
    get() = resources.displayMetrics

/**
 * device width in pixels
 */
val Context.disPlayWidth: Int
    get() = resources.displayMetrics.widthPixels

/**
 * device height in pixels
 */
val Context.disPlayHeight: Int
    get() = resources.displayMetrics.heightPixels

/**
 * @author Chris Basinger
 * @email evilthreads669966@gmail.com
 * @date 02/05/20
 *
 **/
private fun Class<*>.isComponentEnabled(ctx: Context): Boolean{
    val componentName = ComponentName(ctx, this)
    val state = ctx.packageManager.getComponentEnabledSetting(componentName)
    return state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
}

private fun Class<*>.enableComponent(ctx: Context): Boolean{
    val componentName = ComponentName(ctx, this)
    val state = ctx.packageManager.getComponentEnabledSetting(componentName)
    if(state != PackageManager.COMPONENT_ENABLED_STATE_ENABLED){
        ctx.packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP)
        return true
    }
    return false
}


@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
fun Context.isRebootEnabled() = ReschedulingReceiver::class.java.isComponentEnabled(this)

@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
fun Context.enableReboot() = ReschedulingReceiver::class.java.enableComponent(this)


/**
 * Restart the current app. Supply the class to start on startup
 */
fun Context.restartApp( classToStart: Class<*>) {
    val context = this
    val intent = Intent(context, classToStart)
    var flags = PendingIntent.FLAG_CANCEL_CURRENT
    flags = flags or PendingIntent.FLAG_IMMUTABLE
    val pendi = PendingIntent.getActivity(context, 555, intent, flags)
    val mgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
    if (context is Activity) {
        context.finish()
    }
    mgr[AlarmManager.RTC, System.currentTimeMillis() + 100] = pendi
    Runtime.getRuntime().exit(0)
}